package com.cgmnx.admin.biz.login.impl;

import com.cgmnx.admin.biz.login.LoginBiz;
import com.cgmnx.admin.common.util.EncryptionUtil;
import com.cgmnx.admin.common.util.WebUtils;
import com.cgmnx.admin.dao.entity.rbac.OperationLogEntity;
import com.cgmnx.admin.dao.entity.rbac.UserEntity;
import com.cgmnx.admin.model.enums.rbac.OperationType;
import com.cgmnx.admin.service.rbac.OperationLogService;
import com.cgmnx.admin.service.rbac.UserService;
import com.google.common.base.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 *
 */
@Service
@Slf4j
public class LoginBizImpl implements LoginBiz {

    @Autowired
    private UserService userService;

    @Autowired
    private OperationLogService operationLogService;

    @Value("${login.secret}")
    private String secret;


    @Value("${login.appKey}")
    private String appKey;

    @Value("${login.qrcode.img.url}")
    private String qrCodeImgUrl;

    @Value("${login.token.url}")
    private String tokenUrl;

    @Value("${login.accToken.url}")
    private String accTokenUrl;


    @Override
    public Optional<UserEntity> login(String userId, String password) {

        Optional<UserEntity> optional = userService.queryUserByUserId(userId);

        if (!optional.isPresent()) {
            return Optional.absent();
        }

        if (log.isDebugEnabled()) {
            log.debug("根据userId查询的用户信息:{},{},{}", userId, password, optional.get());
        }

        UserEntity userEntity = optional.get();

        String encryptedPassword = EncryptionUtil.encryptUserInfo(userEntity.getUserId(), password, userEntity.getSalt());

        if (log.isDebugEnabled()) {
            log.debug("根据userId生成的密码:{}", encryptedPassword);
        }

        if (StringUtils.equals(encryptedPassword, userEntity.getPassword())) {
            //添加登陆日志
            saveLog(userId);
            return optional;
        }
        return Optional.absent();
    }


    public void saveLog(String userId) {
        try {
            // 获取request对象
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            //获取请求ip
            String ip = WebUtils.getRealIP(request);

            OperationLogEntity logEntity = new OperationLogEntity();
            logEntity.setContent("登陆系统");
            logEntity.setOperationType(OperationType.LOGIN);
            logEntity.setOperatorName(userId);
            logEntity.setIp(ip);

            logEntity.setCreateUser(userId);
            logEntity.setUpdateUser(userId);

            operationLogService.save(logEntity);
        } catch (Exception e) {
            log.info("登录时候错误,", e);
        }

    }

}